
import { WX_AD_CONFIG, Sdk_Platform } from '../../frame/constants';
cc.Class({
    extends: cc.Component,

    properties: {
        topNode: cc.Node,
        bottomNode: cc.Node,

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},

    initPage() {
        if (!he.pool.getPool('scrollAd')) {
            he.pool.createPoolSync('scrollAd', 5, cc.Prefab, 'uimod/scrollAd')
        }
        if (!he.pool.getPool('adIcon')) {
            he.pool.createPoolSync('adIcon', 10, cc.Prefab, 'uimod/adIcon')
        }
    },

    updatePage(param) {
        this.adSwitch(true)
    },

    closeUI() {
        // for (let i = (this.content.children.length); i > 0; i--) {
        //     he.pool.putElement('roleitemPre', this.content.children[i - 1])
        // }
        for (let i = (this.topNode.children.length); i > 0; i--) {
            this.topNode.children[i - 1].getComponent('scrollAd').clearItem()
            he.pool.putElement('scrollAd', this.topNode.children[i - 1])
        }

        for (let j = (this.bottomNode.children.length); j > 0; j--) {
            this.bottomNode.children[j - 1].getComponent('scrollAd').clearItem()
            he.pool.putElement('scrollAd', this.bottomNode.children[j - 1])
        }
    },
    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    if (!common.showAd) return
                    he.ad.getTdAdJson(WX_AD_CONFIG.tmIcon[1], this.topSet.bind(this))
                    he.ad.getTdAdJson(WX_AD_CONFIG.tmIcon[2], this.bottomSet.bind(this))
                    break
            }
        } else {
            //  关掉广告
        }
    },



    topSet(config) {
        // console.log(config)
        if (config.isOpen) {
            let pre = he.pool.getElement('scrollAd')
            pre.parent = this.topNode
            pre.getComponent('scrollAd').init(config.creatives, 1, true)
        }
    },
    bottomSet(config) {
        if (config.isOpen) {
            let pre = he.pool.getElement('scrollAd')
            pre.parent = this.bottomNode
            pre.getComponent('scrollAd').init(config.creatives, 1, true)
        }
    },
});
